package com.example.dishit.controller.recommendation;

import android.content.Intent;
import android.net.Uri;

import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dish.DishData;
import com.example.dishit.service.recommendation.IRecommendationListener;
import com.example.dishit.service.recommendation.RecommendationGeneratorFactory;
import com.example.dishit.view.recommendation.RecommendationActivity;

public class RecommendationController implements IRecommendationController {
    private final RecommendationActivity view;
    private final DishData restaurant;
    
    public RecommendationController(final RecommendationActivity view,
            final DishData restaurant) {
        this.view = view;
        this.restaurant = restaurant;
    }
    
    public void close() {
        view.finish();
    }
    
    public void dialRestaurant() {
        final String phoneNumDigits = restaurant.getPhoneNumberDigits();
        if(phoneNumDigits == null || phoneNumDigits.isEmpty()) return;
        
        Uri uri = Uri.parse("tel:" + phoneNumDigits);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        view.startActivity(intent);
    }
    
    // FIXME: Is there a way to open up Google Navigator/Maps instead???
    public void openMap() {
        final String mapUrl = restaurant.getMapUrl();
        if(mapUrl == null || mapUrl.isEmpty()) return;
        
        Uri uri = Uri.parse(mapUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        view.startActivity(intent);
    }
    
    public void openWebsite() {
        final String websiteUrl = restaurant.getWebsiteUrl();
        if(websiteUrl == null || websiteUrl.isEmpty()) return;
        
        Uri uri = Uri.parse(websiteUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        view.startActivity(intent);
    }
    
    public void tryAgain() {
        final IRecommendationListener callback =
                new RecommendationListener(view, MainController.THIS_USER_ID);
        RecommendationGeneratorFactory.instance().requestRecommendation(callback);
    }
}
